﻿
Imports std
Imports storoomc

Partial Public MustInherit Class storoomOper(Of THREADMODEL As storoomThreadModel)
    'must overrides functions
    Public MustOverride Function showslice(ByVal slice As storageInterface) As String

    'overridable
    Protected Overridable Function selectSlices(ByVal key As String) As vector(Of storageInterface)
        Return slices
    End Function

    Protected Overridable Function fullImpl(ByVal slices As vector(Of storageInterface)) As ternary
        Return tm.full(slices)
    End Function

    Protected Overridable Function retireImpl(ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.retire(slices)
    End Function

    Protected Overridable Function emptyImpl(ByVal slices As vector(Of storageInterface)) As ternary
        Return tm.empty(slices)
    End Function

    Protected Overridable Function closeImpl(ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.close(slices)
    End Function

    Protected Overridable Function openImpl(ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.open(slices)
    End Function

    Protected Overridable Function listImpl(ByVal l As vector(Of String),
                                            ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.list(l, slices)
    End Function

    Protected Overridable Function seekImpl(ByVal key As String, ByVal slices As vector(Of storageInterface)) As ternary
        Return tm.seek(key, slices)
    End Function

    Protected Overridable Function readImpl(ByVal key As String,
                                            ByRef buff As bytes,
                                            ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.read(key, buff, slices)
    End Function

    Protected Overridable Function deleteImpl(ByVal key As String,
                                              ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.delete(key, slices)
    End Function

    Protected Overridable Function sizeofImpl(ByVal key As String,
                                              ByRef size As UInt64,
                                              ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.sizeof(key, size, slices)
    End Function

    Protected Overridable Function capacityImpl(ByRef cap As UInt64,
                                                ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.capacity(cap, slices)
    End Function

    Protected Overridable Function valuesizeImpl(ByRef sz As UInt64,
                                                 ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.valuesize(sz, slices)
    End Function

    Protected Overridable Function keycountImpl(ByRef count As UInt64,
                                                ByVal slices As vector(Of storageInterface)) As Boolean
        Return tm.keycount(count, slices)
    End Function

    Protected Overridable Function writeImpl(ByVal key As String, ByVal buff As bytes, _
                                             ByRef slice As storageInterface, _
                                             ByVal slices As vector(Of storageInterface)) As Boolean
        slice = randomNotFullSlice(slices, Nothing)
        If slice Is Nothing Then
            raiseError("cannot get an empty and health slice to write in.", errorHandle.errorType.exclamation)
            Return False
        Else
            If tm.write(key, buff, slice) Then
                Return True
            Else
                raiseError("cannot write data to a not full slice, key " + key + ", slice " + showslice(slice), _
                           errorHandle.errorType.exclamation)
                Return False
            End If
        End If
    End Function

    Protected Overridable Function modifyImpl(ByVal key As String, ByVal buff As bytes, _
                                              ByRef slice As storageInterface, _
                                              ByVal slices As vector(Of storageInterface)) As Boolean
        Dim rtn As ternary = Nothing
        rtn = tm.seek(key, slices)
        If rtn = ternary.true Then
            Return _modify(key, buff, slice, slices)
        ElseIf rtn = ternary.false Then
            Return writeImpl(key, buff, slice, slices)
        Else
            Return False
        End If
    End Function
End Class
